//
//  BangoAnalytics.h
//  Bango
//

#import <Foundation/Foundation.h>
#import "Profile.h"
#import "EventParameter.h"
#import <CoreLocation/CoreLocation.h>


@interface BangoAnalyticsManager : NSObject <UIWebViewDelegate, CLLocationManagerDelegate> {
	
	@private
	
	// for persistance
	NSManagedObjectContext *managedObjectContext;

	// config options
	NSString *applicationChannel;
	NSString *applicationType;
	NSString *applicationVersion;
	int continueSessionSeconds;
	bool findLocation;
	bool logEnabled;
	bool automaticDataTransmission;
	NSString *postUrl;
	NSString *userId;
	NSString *ipAddress;
	
	// stores if the device can make calls
	bool deviceIsMobile;

	// For obtaining useragent
	NSString *userAgent;
	UIWebView *webView;
	
	// pointer for profile being submitted
	Profile *transmittingProfile;
	
	// core locations manager
	CLLocationManager *locationManager;
	
	// for transmission
	bool transmitting;
	
	// for debug log
	bool debugLog;
	
	// stores the current transmission mode.
	NSString *transmissionMode;
	
	// The data being asynchronously received from bango
	NSMutableData *responseData; 
}

////////////////////////////////////////////////////////////////////////////
// accessor generators

// the fields dictionaries
@property (nonatomic, retain) NSMutableDictionary *profileFieldsDictionary;
@property (nonatomic, retain) NSMutableDictionary *eventFieldsDictionary;

// for database persistance
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

// for config
@property (nonatomic, retain) NSString *applicationChannel;
@property (nonatomic, retain) NSString *applicationType;
@property (nonatomic, retain) NSString *applicationVersion;
@property (nonatomic, readwrite) int continueSessionSeconds;
@property (nonatomic, readwrite) bool findLocation;
@property (nonatomic, readwrite) bool logEnabled;
@property (nonatomic, readwrite) bool automaticDataTransmission;
@property (nonatomic, retain) NSString *postUrl;
@property (nonatomic, retain) NSString *userId;
@property (nonatomic, retain) NSString *ipAddress;

@property (nonatomic, readwrite) bool deviceIsMobile;

// useragent
@property (nonatomic, retain) NSString *userAgent;

// transmitting profile
@property (nonatomic, retain) Profile *transmittingProfile;
@property (nonatomic, retain) NSMutableData *responseData;

// core locations
@property (nonatomic, retain) CLLocationManager *locationManager;

// transmitting
@property (nonatomic, readwrite) bool transmitting;


// transmission mode
@property (nonatomic, retain) NSString *transmissionMode;

// debug
@property (nonatomic, readwrite) bool debugLog;

////////////////////////////////////////////////////////////////////////////
// selectors

/*
 * Session Methods: These are to do with the Session Profile. i.e. starting a session (creating a new profile) and ending a session (attempting to transmit
 * the profile info).
 */

// This method initiates logging, this must be called after the configuration methods but before any other logging or any other session methods.
-(void)onStartSessionWithApplicationId :(NSString*)applicationId;
-(void)onStartSessionWithApplicationId :(NSString*)applicationId eventParameters:(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method indicates that the session has ended and will initiate the transmitting of data to the specified url.
-(void)onEndSession;
-(void)onEndSessionWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method indicates that the session has entered an idle state, for example the application has lost focus.
-(void)onIdle;
-(void)onIdleWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method indicates that the session has resumed after an idle state, for example the application has gained focus after losing focus.
-(void)onResume;
-(void)onResumeWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// creates an event for a profile
// can be used to create any type of event
-(Event*) createEventForProfile :(Profile*)profile withEventName:(NSString *)eventName eventDetail:(NSString *)eventDetail eventValue:(NSString *)eventValue 
				 eventParameters:(NSDictionary*)eventParameters;

-(Event*) createEventForProfile :(Profile*)profile withEventName:(NSString *)eventName eventDetail:(NSString *)eventDetail eventValue:(NSString*)eventValue
				 eventErrorLevel:(NSNumber*)eventErrorLevel eventErrorId:(NSString*)eventErrorId 
				 eventParameters:(NSDictionary*)eventParameters;

// Creates an event parameter for an event
-(EventParameter*) createEventParameterForEvent :(Event*)event withEventName:(NSString *)eventName eventValue:(NSString *)eventValue;

// method which creates an event for the current profile
// can be used to create any kind of event
-(void)createEventForCurrentProfile :(NSString *)eventName eventDetail:(NSString *)eventDetail eventValue:(NSString*)eventValue
					 eventErrorLevel:(NSNumber*)eventErrorLevel eventErrorId:(NSString*)eventErrorId 
					 eventParameters:(NSDictionary*)eventParameters;


/*
 * Logging Methods: These are to do with logging events such as video start and stop
 */

// This method logs an onDemandVideoStart event.
-(void)onDemandVideoStart;
-(void)onDemandVideoStartWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method logs an onDemandVideoStop event.
-(void)onDemandVideoStop;
-(void)onDemandVideoStopWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method logs an onLiveVideoStart event.
-(void)onLiveVideoStart;
-(void)onLiveVideoStartWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method logs an onLiveVideoStop event.
-(void)onLiveVideoStop;
-(void)onLiveVideoStopWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method increments the page view count.
-(void)onPageView;
-(void)onPageViewWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// This method logs a search event.
-(void)onSearch;
-(void)onSearchWithEventParameters :(NSDictionary*)eventParameters eventDetail:(NSString*)eventDetail eventValue:(NSString*)eventValue;

// method which creates an event of any kind - including custom named events
-(void)onEvent :(NSString *)eventName eventDetail:(NSString *)eventDetail eventValue:(NSString*)eventValue eventParameters:(NSDictionary*)eventParameters;

// This method logs an error event.
-(void)onError;
-(void)onErrorWithEventParameters :(NSDictionary*)eventParameters message:(NSString*)message eventClass:(NSString*)eventClass eventErrorId:(NSString*)eventErrorId;
-(void)onErrorWithEventParameters :(NSDictionary*)eventParameters message:(NSString*)message eventClass:(NSString*)eventClass 
				   eventErrorLevel:(NSNumber*)eventErrorLevel eventErrorId:(NSString*)eventErrorId;

/*
 * Helper methods
 */

// constructor
- (id) initWithManagedObjectContext:(NSManagedObjectContext*)moContext;

// obtaian a reference to the singleton
+ (id)sharedManager;

// for useragent stuff
-(NSString*)userAgentString;

// kicks of a transmission run - setting the appropriate mode as it does so
// this should be a private method
-(void)commenceTransmissionWithMode:(NSString*)mode;

// kicks off process to transmit all outstanding analytics data to bango
-(void)transmitAll;

// transmits analytics data to bango for specific profile
-(void)transmitProfile:(Profile*)profile;

// deletes the specified profile
-(bool)deleteProfile:(Profile*)profile;

// deletes the events in tspecified profile
-(bool)deleteEventsInProfile:(Profile*)profile;

// logs to debug
-(void)debug:(char*)file lineNumber:(int)lineNumber format:(NSString *)format, ...;


@end
